﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.net.*;
	import flash.geom.*;
	import flash.filters.*;
	import flash.events.*;
	import fl.controls.*;
	import fl.events.*;
	public class Tracer extends Sprite {
		private var conn:LocalConnection;
		private var output:TextArea;
		private var bt_stop:Button;
		private var bt_clear:Button;
		private var defaultlc:String = "_cenfun_lc";
		private var realtime:Boolean = true;
		public function Tracer() {
			stage.scaleMode = "noScale";
			stage.align = "topLeft";
			stage.addEventListener(Event.RESIZE, stageResize);
			buildUI();
			conn = new LocalConnection();
			conn.allowDomain("*");
			conn.client = this;
			conn.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
			conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			try {
				conn.connect(defaultlc);
			} catch (error:ArgumentError) {
				trace("Can't connect...the connection name is already being used by another SWF");
			}
		}
		private function errorHandler(e:Event):void {
		}
		public function cenfunTrace(msg:String):void {
			if (realtime) {
				output.appendText(msg + "\n");
				scrollHandler();
			}
		}
		private function buildUI():void {
			var logo:Shape = getCenFunLogo(35);
			var cenfun:Sprite = new Sprite();
			cenfun.addChild(logo);
			cenfun.filters = getFilter(0x000000, .5);
			cenfun.x = 25;
			cenfun.y = 20;
			cenfun.buttonMode = true;
			cenfun.addEventListener(MouseEvent.CLICK, showCode);
			addChild(cenfun);
			var cmpTitle:TextField = new TextField();
			cmpTitle.selectable = false;
			cmpTitle.autoSize = "left";
			cmpTitle.text = "Tracer";
			cmpTitle.setTextFormat(new TextFormat("Verdana", 24, 0x666666, true, false, false));
			cmpTitle.filters = getFilter(0x000000, .2);
			cmpTitle.x = 50;
			cmpTitle.y = 5;
			addChild(cmpTitle);
			//
			output = new TextArea();
			output.setStyle("textFormat", new TextFormat("Verdana", 12, 0x000000, false, false, false));
			output.move(10, 40);
			output.addEventListener(FocusEvent.FOCUS_IN, clearFocus);
			addChild(output);
			//
			bt_stop = new Button();
			bt_stop.label = "Stop";
			bt_stop.setStyle("textFormat", new TextFormat("Verdana", 10, 0x000000, true, false, false));
			bt_stop.addEventListener(MouseEvent.CLICK, stopOutput);
			addChild(bt_stop);
			//
			bt_clear = new Button();
			bt_clear.label = "Clear";
			bt_clear.setStyle("textFormat", new TextFormat("Verdana", 10, 0x000000, true, false, false));
			bt_clear.addEventListener(MouseEvent.CLICK, clearOutput);
			addChild(bt_clear);
			//
			layout();
		}
		private function stopOutput(e:Event):void {
			if (e.target.label == "Stop") {
				realtime = false;
				e.target.label = "Start";
			} else {
				realtime = true;
				e.target.label = "Stop";
			}
		}
		private function clearOutput(e:Event):void {
			output.text = "";
		}
		private function showCode(e:MouseEvent):void {
			output.appendText('//-------------------------------------------------------------------------\n');
			output.appendText('//输出函数(output)\n');
			output.appendText('function output(str:Object):void{;\n');
			output.appendText('  var lc:LocalConnection = new LocalConnection();\n');
			output.appendText('  lc.addEventListener(StatusEvent.STATUS, function(e:Event):void{});\n');
			output.appendText('  var s:String = str.toString();\n');
			output.appendText('  trace(s);\n');
			output.appendText('  lc.send("_cenfun_lc", "cenfunTrace", s);\n');
			output.appendText('}\n');
			output.appendText('//调用：\n');
			output.appendText('//output("helloWorld!");\n');
			output.appendText('//-------------------------------------------------------------------------\n');
			scrollHandler();
		}
		//cenfun logo
		private function getCenFunLogo(tw:Number):Shape {
			var logo:Shape = new Shape();
			var th:Number = tw*3/4;
			var tr:Number = tw/4;
			var rd1:Number = Math.atan(3);
			var rd2:Number = Math.atan(3/8);
			var ed1:Number = Math.atan(1/4);
			var matr:Matrix = new Matrix();
			matr.createGradientBox(tw, th, Math.PI/2, -tw/2, -th/2);
			logo.graphics.beginGradientFill(GradientType.LINEAR, [0x5fb45f, 0x008800], [1, 1], [0, 255], matr, SpreadMethod.PAD);
			logo.graphics.lineStyle(1,0x008800,.5);
			logo.graphics.moveTo(tr, 0);
			for (var i:Number = rd1; i <= (2*Math.PI-rd2); i = (i+.1)) {
				logo.graphics.lineTo(tr+Math.cos(-i)*tr, Math.sin(-i)*tr);
			}
			for (var j:Number = (2*Math.PI-ed1); j >= (ed1-.1); j = (j-.1)) {
				logo.graphics.lineTo(Math.cos(-j)*tw/2, Math.sin(-j)*th/2);
			}
			logo.graphics.lineTo(tr, 0);
			logo.graphics.endFill();
			return logo;
		}
		//滤镜
		private function getFilter(c:Number,a:Number):Array {
			return new Array(new DropShadowFilter(1,45,c,a,4,4,1,1));
		}
		//stage
		private function stageResize(e:Event):void {
			layout();
		}
		private function clearFocus(e:FocusEvent):void {
			output.drawFocus(false);
		}
		private function scrollHandler():void {
			output.setFocus();
			output.verticalScrollPosition = output.maxVerticalScrollPosition;
		}
		private function layout():void {
			output.setSize(stage.stageWidth - 20, stage.stageHeight - 50);
			bt_stop.move(stage.stageWidth-220, 10);
			bt_clear.move(stage.stageWidth-110, 10);
			scrollHandler();
		}
	}
}